/**
 * 中国9号健康产业有限公司
 * Copyright (C) JiuHaoHealth Corporation. All Rights Reserved
 */
package com.jiuhaohealth.common.util;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import net.sf.json.JSONObject;
import net.sf.json.filters.MyFilter;

import org.apache.commons.beanutils.BeanUtils;

import com.google.gson.Gson;
/**
 * @author minghui(jackchenmh@gmail.com)
 * @version 2014-3-18 下午5:08:45
 */
public class JsonSerializableUtil implements SerializableUtil {

	public final static JsonSerializableUtil jsonSerializableUtil
							= new JsonSerializableUtil();
	
	private static Map<String, Class<?>> classMap = 
						new ConcurrentHashMap<String, Class<?>>();
	
	private Gson gson = new Gson();
	
	@Override
	public Object fromSerializable(String s) {
		JSONObject jObject = JSONObject.fromObject(s);
		String clazzs = jObject.getString("javaclass");
		String valueJson = jObject.getString("value");
		Object obj = null;
		try {
			if(clazzs.startsWith("list;")) {
				String beanClass = clazzs.substring(5);
				if(!beanClass.equals("")) {
					Class<?> clazz = classMap.get(beanClass);
					if(clazz == null) {
						clazz = Thread.currentThread().getContextClassLoader().loadClass(beanClass);
						classMap.put(beanClass, clazz);
					}
	//				List<MorphDynaBean> ls = 
	//					JsonUtil.jsonArrayToList(valueJson, MorphDynaBean.class, new MyFilter(false, "table", "tableOffset"));
	//				List<Object> rs = new ArrayList<Object>();
	//				for (MorphDynaBean map : ls) {
	//					rs.add(_ParamsToBean(map, clazz));
	//				}
	//				obj = rs;
	//				//
					obj = JsonUtil.jsonArrayToList(valueJson, clazz);
				} else {
					obj = new ArrayList();
				}
			} else {
				if(!clazzs.startsWith("java")) {
					Class<?> clazz = Thread.currentThread().getContextClassLoader().loadClass(clazzs);
					obj = JsonUtil.getObjectFromJson(valueJson, clazz);
				} else {
					obj = valueJson;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public String toSerializable(Object obj) {
		String clazzs = "";
		if(obj instanceof List<?>) {
			List<?> list = (List<?>)obj;
			if(list.size() > 0)
				clazzs = "list;" + list.get(0).getClass().getName();
			else
				clazzs = "list;";
		} else {
			clazzs = obj.getClass().getName();
		}
		JsonSerializable jsonSerializable = new JsonSerializable(clazzs, obj);
		String json = JsonUtil.getJsonFromObject(jsonSerializable, new MyFilter(false, "table", "tableOffset"));
		return json;
	}

	public static <T> T mapToBean(Map<String, Object> params , Class<T> beanClass) {
		//生成Bean
		Map<String, Object> new_params = null;
		try{
			new_params = params;
			T bean = beanClass.newInstance();
			BeanUtils.populate(bean, new_params);
			return bean;
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}
	
	public static Map<String, Object> beanToMap(Object obj) {
		try {
			return BeanUtils.describe(obj);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		}
		return null;
	}
}
